package Luowenjin;

import java.util.Scanner;


public class DoubleColorBall {
    public static void main(String[] args) {

        // 生成中奖号码
        int[] winningNumbers = generateWinningNumbers();
        // 获取用户输入的号码
        int[] userNumbers = getUserInput();

        // 输出中奖号码和用户号码
        System.out.println("中奖号码为:");
        printNumbers(winningNumbers);
        System.out.println("您投注号码为:");
        printNumbers(userNumbers);

        // 计算命中的红球和蓝球数量
        int redHitCount = countHitNumbers(winningNumbers, userNumbers, 0, 6);
        int blueHitCount = countHitNumbers(winningNumbers, userNumbers, 6, 7);

        // 输出命中球数
        System.out.println("您命中了" + redHitCount + "个红色球号码, " + blueHitCount + "个蓝色球号码。");

        // 判断中奖级别
        String prizeLevel = determinePrizeLevel(redHitCount, blueHitCount);
        System.out.println(prizeLevel);
    }

    // 生成中奖号码
    public static int[] generateWinningNumbers() {
        int[] numbers = new int[7];
        // 生成6个不重复的红球号码
        for (int i = 0; i < 6; i++) {
            while (true) {
                int num = (int) (Math.random() * 33 + 1);
                boolean isDuplicate = false;
                for (int j = 0; j < i; j++) {
                    if (numbers[j] == num) {
                        isDuplicate = true;
                        break;
                    }
                }
                if (!isDuplicate) {
                    numbers[i] = num;
                    break;
                }
            }
        }

        // 生成蓝球号码
        numbers[6] = (int) (Math.random() * 16 + 1);
        return numbers;
    }

    // 获取用户输入的投注号码
    public static int[] getUserInput() {
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[7];
        // 获取用户输入的6个红球号码
        for (int i = 0; i < 6; i++) {
            while (true) {
                System.out.println("请输入红球号码, 输入任意一个范围1-33的数字:");
                int num = scanner.nextInt();
                if (num < 1 || num > 33) {
                    System.out.println("输入的数字不在1-33范围内，请重新输入！");
                    continue;
                }
                boolean isDuplicate = false;
                for (int j = 0; j < i; j++) {
                    if (numbers[j] == num) {
                        isDuplicate = true;
                        System.out.println("输入的红球号码已存在，请重新输入！");
                        break;
                    }
                }
                if (!isDuplicate) {
                    numbers[i] = num;
                    break;
                }
            }
        }
        // 获取用户输入的蓝球号码
        while (true) {
            System.out.println("请输入蓝球号码, 输入任意一个范围1-16的数字:");
            int num = scanner.nextInt();
            if (num < 1 || num > 16) {
                System.out.println("输入的数字不在1-16范围内，请重新输入！");
                continue;
            }
            numbers[6] = num;
            break;
        }
        return numbers;
    }

    // 计算命中的球数
    public static int countHitNumbers(int[] winningNumbers, int[] userNumbers, int startIndex, int endIndex) {
        int count = 0;
        for (int i = startIndex; i < endIndex; i++) {
            for (int j = startIndex; j < endIndex; j++) {
                if (winningNumbers[i] == userNumbers[j] ) {
                    count++;
                    break;
                }
            }
        }
        return count;
    }

    // 判断中奖级别
    public static String determinePrizeLevel(int redHitCount, int blueHitCount) {
        if (redHitCount == 6 && blueHitCount == 1) {
            return "恭喜您，中了一等奖！";
        } else if (redHitCount == 6 && blueHitCount == 0) {
            return "恭喜您，中了二等奖！";
        } else if ((redHitCount == 5 && blueHitCount == 1) || (redHitCount == 4 && blueHitCount == 1)) {
            return "恭喜您，中了三等奖！";
        } else if ((redHitCount == 5 && blueHitCount == 0) || (redHitCount == 4 && blueHitCount == 0) || (redHitCount == 3 && blueHitCount == 1)) {
            return "恭喜您，中了四等奖！";
        } else if ((redHitCount == 2 && blueHitCount == 1) || (redHitCount == 1 && blueHitCount == 1) || (redHitCount == 0 && blueHitCount == 1)) {
            return "恭喜您，中了五等奖！";
        } else {
            return "很遗憾，您未中奖！";
        }
    }

    // 打印数组中的号码
    public static void printNumbers(int[] numbers) {
        System.out.print("[");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i]);
            if (i < numbers.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("]");
    }
}




